
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather.plugin.net;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.concurrent.TimeUnit;

import com.tompai.datagather.conf.DefaultConfigured;
import com.tompai.datagather.model.CrawlDatum;
import com.tompai.datagather.model.Page;
import com.tompai.datagather.net.Requester;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
* @author: tompai
* @createTime: 2018年4月28日 下午7:42:41
* @history:
* @version: v1.0
*/
public class OkHttpRequester extends DefaultConfigured implements Requester{



    protected OkHttpClient client;

    protected HashSet<Integer> successCodeSet;

    public OkHttpRequester addSuccessCode(int successCode){
        successCodeSet.add(successCode);
        return this;
    }
    public OkHttpRequester removeSuccessCode(int successCode){
        successCodeSet.remove(successCode);
        return this;
    }


    protected HashSet<Integer> createSuccessCodeSet(){
        HashSet<Integer> result = new HashSet<Integer>();
        result.add(200);
        result.add(301);
        result.add(302);
        result.add(404);
        return result;
    }



    public OkHttpClient.Builder createOkHttpClientBuilder(){
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .followRedirects(false)
                .followSslRedirects(false)
                .connectTimeout(getConf().getConnectTimeout(), TimeUnit.MILLISECONDS)
                .readTimeout(getConf().getReadTimeout(), TimeUnit.MILLISECONDS);
        return builder;

    }

    public Request.Builder createRequestBuilder(CrawlDatum crawlDatum){
        Request.Builder builder = new Request.Builder()
                .header("User-Agent",getConf().getDefaultUserAgent())
                .url(crawlDatum.url());
        return builder;
    }

    public OkHttpRequester() {
        successCodeSet = createSuccessCodeSet();
        client = createOkHttpClientBuilder().build();
    }

    @Override
    public Page getResponse(CrawlDatum crawlDatum) throws Exception {
        Request  request = createRequestBuilder(crawlDatum).build();
        Response response = client.newCall(request).execute();

        String contentType = null;
        byte[] content = null;
        String charset = null;

        ResponseBody responseBody = response.body();
        int code = response.code();
        if(!successCodeSet.contains(code)){
//            throw new IOException(String.format("Server returned HTTP response code: %d for URL: %s (CrawlDatum: %s)", code,crawlDatum.url(), crawlDatum.key()));
            throw new IOException(String.format("Server returned HTTP response code: %d for %s", code, crawlDatum.briefInfo()));

        }
        if(responseBody != null){
            content = responseBody.bytes();
            MediaType mediaType = responseBody.contentType();
            if(mediaType!=null){
                contentType = mediaType.toString();
                Charset responseCharset = mediaType.charset();
                if(responseCharset!=null){
                    charset = responseCharset.name();
                }
            }
        }

        Page page = new Page(
                crawlDatum,
                code,
                contentType,
                content
                );
        page.charset(charset);
        page.obj(response);
        return page;
    }

    public HashSet<Integer> getSuccessCodeSet() {
        return successCodeSet;
    }

    public void setSuccessCodeSet(HashSet<Integer> successCodeSet) {
        this.successCodeSet = successCodeSet;
    }
}
